﻿using System;
using System.Runtime.InteropServices.ComTypes;
using Dan.Common;
using Dapper;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace rabbitmqdemo.Controllers
{
    [Route("api/redis")]
    public class RedisController : Controller
    {
        private string connStr =
            "server=192.168.3.109;userid=root;pwd=Rp000000;port=3306;database=rpdatabase;SslMode=none;";

        private string key1 = "rpdatabase:basestaff:hash";
        private string key2 = "rpdatabase:basestaff:string";

        [Route("set1")]
        public Object Set1()
        {
            using (var conn = new MySqlConnection(connStr))
            {
                var list = conn.Query<Basedept>("select * from basedept");

                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.3.34:6379"))
                {
                    var db = redis.GetDatabase();
                    foreach (var item in list)
                    {
                        db.HashSet($"{key1}{item.Id}", new HashEntry[]
                        {
                            new HashEntry("Id", item.Id.SafeToString()),
                            new HashEntry("DeptName", item.DeptName.SafeToString()),
                            new HashEntry("DeptAbbreviation", item.DeptAbbreviation.SafeToString()),
                            new HashEntry("ParentId", item.ParentId.SafeToString()),
                            new HashEntry("DeptType", item.DeptType.SafeToString()),
                            new HashEntry("Manager", item.Manager.SafeToString()),
                            new HashEntry("CompanyId", item.CompanyId.SafeToString()),
                            new HashEntry("CityId", item.CityId.SafeToString()),
                            new HashEntry("DeptTel", item.DeptTel.SafeToString()),
                            new HashEntry("Address", item.Address.SafeToString()),
                            new HashEntry("Remarks", item.Remarks.SafeToString()),
                            new HashEntry("Img", item.Img.SafeToString()),
                            new HashEntry("Thumbnail", item.Thumbnail.SafeToString()),
                            new HashEntry("Longitude", item.Longitude.SafeToString()),
                            new HashEntry("Latitude", item.Latitude.SafeToString()),
                            new HashEntry("IsEnable", item.IsEnable.SafeToString()),
                            new HashEntry("TerminalCode", item.TerminalCode.SafeToString()),
                            new HashEntry("IsShow", item.IsShow.SafeToString()),
                            new HashEntry("Slogan", item.Slogan.SafeToString()),
                            new HashEntry("CodeImg", item.CodeImg.SafeToString()),
                            new HashEntry("IsShowInChangePrice", item.IsShowInChangePrice.SafeToString()),
                        });

                        var values = db.HashGet($"{key1}{item.Id}", new RedisValue[]
                        {
                            "Id",
                            "DeptNa me",
                            "DeptAbbreviation",
                            "ParentId",
                            "DeptType",
                            "Manager",
                            "CompanyId",
                            "CityId",
                            "DeptTel",
                            "Address",
                            "Remarks",
                            "Img",
                            "Thumbnail",
                            "Longitude",
                            "Latitude",
                            "IsEnable",
                            "TerminalCode",
                            "IsShow",
                            "Slogan",
                            "CodeImg",
                            "IsShowInChangePrice",
                        });

                        var dept = new Basedept();
                        dept.Id = values[0].SafeToInt();
                        dept.DeptName = values[1];
                        dept.DeptAbbreviation = values[2];
                        dept.ParentId = values[3].SafeToInt();
                        dept.DeptType = values[4];
                        dept.Manager = values[5];
                        dept.CompanyId = values[6].SafeToInt();
                        dept.CityId = values[7].SafeToInt();
                        dept.DeptTel = values[8];
                        dept.Address = values[9];
                        dept.Remarks = values[10];
                        dept.Img = values[11];
                        dept.Thumbnail = values[12];
                        dept.Longitude = values[13];
                        dept.Latitude = values[14];
                        dept.IsEnable = values[15];
                        dept.TerminalCode = values[16];
                        dept.IsShow = values[17];
                        dept.Slogan = values[18];
                        dept.CodeImg = values[19];
                        dept.IsShowInChangePrice = values[20];

                        Console.WriteLine(dept);
                    }
                }
            }

            return "保存到entry中";
        }
        
        [Route("set2")]
        public Object Set2()
        {
            using (var conn = new MySqlConnection(connStr))
            {
                var list = conn.Query<Basedept>("select * from basedept");

                using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.3.34:6379"))
                {
                    var db = redis.GetDatabase();
                    foreach (var item in list)
                    {
                        db.StringSet($"{key2}{item.Id}", JsonConvert.SerializeObject(item));
                        var dept = JsonConvert.DeserializeObject(db.StringGet($"{key2}{item.Id}"));
                        Console.WriteLine(dept);
                    }
                }
            }

            return "保存到entry中";
        }
    }

    public class Basedept
    {
        public int Id { get; set; }


        public string DeptName { get; set; }


        public string DeptAbbreviation { get; set; }


        public int? ParentId { get; set; }


        public string DeptType { get; set; }


        public string Manager { get; set; }


        public int? CompanyId { get; set; }


        public int CityId { get; set; }


        public string DeptTel { get; set; }


        public string Address { get; set; }


        public string Remarks { get; set; }


        public string Img { get; set; }


        public string Thumbnail { get; set; }


        public string Longitude { get; set; }


        public string Latitude { get; set; }


        public string IsEnable { get; set; }


        public string TerminalCode { get; set; }


        public string IsShow { get; set; }


        public string Slogan { get; set; }


        public string CodeImg { get; set; }


        public string IsShowInChangePrice { get; set; }
    }
}